package com.hz.sdka.ada;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.anythink.nativead.api.ATNative;
import com.hz.sdka.commona.api.feed.HZFeedAdView;
import com.hz.sdka.commona.api.feed.OnHZFeedListener;
import com.hz.sdka.commona.api.feed.OnHZFeedLoadListener;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.bean.HZAdInfo;
import com.hz.sdka.commona.utils.ConvertUtils;
import com.hz.sdka.sdka.AdNativea;
import com.hz.sdka.sdka.FeedAda;

/**
 * 信息流广告
 */
public class NativeBannerAda {
    private final String placeId;
    private String TAG = "TopOnAd";
    private FeedAda feedAd;
    private AdListenera adListener;
    private HZFeedAdView hzFeedAdView;


    public NativeBannerAda(Activity activity, String placeId) {
        this.placeId = placeId;
        feedAd = new FeedAda(activity, placeId);
    }

    public void load() {
        feedAd.setListener(new OnHZFeedLoadListener() {
            @Override
            public void onFeedLoadFailed(HZAdError hzAdError) {
                Log.i(TAG, "onFeedLoadFailed " + hzAdError.printStackTrace());
                if (adListener != null) adListener.adError(hzAdError);
            }

            @Override
            public void onFeedLoaded(AdNativea nativeAdSDK) {
                Log.i(TAG, "onFeedLoaded");
                hzFeedAdView = nativeAdSDK;
                if (adListener != null) adListener.adLoaded();
            }
        });
        if (adListener != null) adListener.adLoading();
        loadAd();
    }

    /*渲染试图*/
    public void showAd(ViewGroup viewGroup, String scenarioId, int layoutId) {
        if (!TextUtils.isEmpty(scenarioId)) {
            ATNative.entryAdScenario(placeId, scenarioId);
        }
        if (adListener != null) adListener.adRequest();
        Log.i(TAG, "showAd");

        if (hzFeedAdView != null) {
            if (adListener != null) adListener.adReady();
            hzFeedAdView.setListener(new OnHZFeedListener() {
                @Override
                public void onFeedShown(HZAdInfo hzAdInfo) {
                    Log.i(TAG, "onFeedShown");
                    if (adListener != null) adListener.adShow();
                }

                @Override
                public void onFeedClosed() {
                    Log.i(TAG, "onFeedClosed");
                    viewGroup.setVisibility(View.GONE);
                    if (adListener != null) adListener.adClose();
                }

                @Override
                public void onFeedClicked() {
                    Log.i(TAG, "onFeedClicked");
                }

            });
            View feedView = hzFeedAdView.getFeedView();
            if (feedView != null) {
                ViewGroup parent = (ViewGroup) feedView.getParent();
                if (parent != null) {
                    parent.removeAllViews();
                }
                viewGroup.removeAllViews();
                viewGroup.addView(feedView);
                int width = ConvertUtils.getScreenWidth();
                hzFeedAdView.render(layoutId, width, scenarioId);
                viewGroup.setVisibility(View.VISIBLE);
            }
        }
    }


    private void loadAd() {
        if (feedAd != null) feedAd.load();
    }

    public void setAdListener(AdListenera adListener) {
        this.adListener = adListener;
    }

    public void onDestory() {
        if (hzFeedAdView != null) hzFeedAdView.destory();
    }
}


